package com.hydl.power.service.impl;

import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageInfo;
import com.hydl.power.mapper.PowerDeviceMapper;
import com.hydl.power.pojo.PowerDevice;
import com.hydl.power.pojo.PowerDeviceExample;
import com.hydl.power.service.PowerDeviceService;

/**
 * @author miss
 * @description 电表设备
 * @date 2023-01-04
 */
@Service
public class PowerDeviceServiceImpl implements PowerDeviceService {

    @Autowired
    private PowerDeviceMapper powerDeviceMapper;

    @Override
    public int insert(PowerDevice powerDevice) {
        return powerDeviceMapper.insertSelective(powerDevice);
    }

    @Override
    public int delete(int id) {
        PowerDeviceExample example = new PowerDeviceExample();
        PowerDeviceExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(id);
        return powerDeviceMapper.deleteByExample(example);
    }

    @Override
    public int update(PowerDevice powerDevice) {
        PowerDeviceExample example = new PowerDeviceExample();
        PowerDeviceExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(powerDevice.getId());
        return powerDeviceMapper.updateByExampleSelective(powerDevice, example);
    }

    @Override
    public PowerDevice load(int id) {
        PowerDeviceExample example = new PowerDeviceExample();
        PowerDeviceExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(id);
        List<PowerDevice> powerDeviceList = powerDeviceMapper.selectByExample(example);
        return CollectionUtils.isEmpty(powerDeviceList) ? null : powerDeviceList.get(0);
    }

    @Override
    public PageInfo<PowerDevice> pageList(int pageNum, int pageSize, PowerDevice powerDevice) {
        PowerDeviceExample example = new PowerDeviceExample();
        // PowerDeviceExample.Criteria criteria = example.createCriteria();
        // criteria.andIdEqualTo(powerDevice.getId());
        List<PowerDevice> pageList = powerDeviceMapper.selectPage(pageNum,pageSize);
        return new PageInfo<>(pageList);
    }

    @Override
    public List<PowerDevice> query(PowerDevice powerDevice) {
        PowerDeviceExample example = new PowerDeviceExample();
        PowerDeviceExample.Criteria criteria = example.createCriteria();
        criteria.andPowerRoomIdEqualTo(powerDevice.getPowerRoomId());
        return powerDeviceMapper.selectByExample(example);
    }

}